function check_email(email) {
	if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email)) {
		return false;
	} else {
		return true;
	}
}
function getNameVal(name) {

	var nameval = $("input[name='" + name + "']:checked").val();
	if (nameval == undefined || nameval == '') {
		return '';
	}
	return nameval;
}
function getSelectTxt(name) {
	var text = $("select[name='" + name + "']").find("option:selected").text();
	return text
}
function getNowTime() {
	var now = new Date();
	var year = now.getYear();
	var month = now.getMonth() + 1;
	var day = now.getDate();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds();
	var nowdate = year + "" + month + "" + day + "" + hour + "" + minute + ""
			+ second;
	return nowdate;
}
$('.checkall').click(function() {
	$('.checkitem').attr('checked', this.checked);
});

function checkAllBox(e) {
	if ($(e).attr('checked') == true) {

		$('.checkitem').attr('checked', 'checked');
	} else {
		$('.checkitem').attr('checked', '');
	}
}
function checkAll() {
	$('.checkitem').attr('checked', 'checked');

}
function unCheckAll() {
	$('.checkitem').attr('checked', '');
}
function getCheckItems() {
	if ($('.checkitem:checked').length == 0) { // 没有选择
		return false;
	}
	var items = '';
	$('.checkitem:checked').each(function() {
		items += this.value + ',';
	});
	items = items.substr(0, (items.length - 1));
	return items;
}
function getHtml(url, id) {
	$("#error").html('加载中...');
	$.get(url, function(rs) {
		$("#" + id).html(rs);
		$("#error").html('');
	});
}
function getUrl(url, reload, cf, confirmMsg) {
	if (cf == 1) {
		if (confirmMsg == undefined || confirmMsg == '') {
			confirmMsg = '确认删除？';
		}
		if (!confirm(confirmMsg)) {
			return false;
		}
	}
	$.get(url, function(rs) {
		showAlert(rs);
		if (reload == 1) {
			setTimeout(function(e) {
				window.location.reload();
			}, 3000)
		}

	});
}
function errAlert(msg) {
	$("body")
			.append(
					'<div id="displayss" style="display:none;width:100%;height:100px;left:0;right:0;top:30%;z-index:999999;position:fixed;" ></div>');
	var disinfo = '<div id = "displayss_con" style="width:470px;height:200px;background:#ffffff;border:solid 3px #eeeeee;margin-left:auto;margin-right:auto;padding:10px"></div>';
	$("#displayss").html(disinfo);
	$("#displayss").show();

	$("#displayss_con").append(
			'<a onclick="' + "hide('displayss');"
					+ '" style="float:right;cursor: pointer">[关闭]</a><br>'
					+ msg);

}
function hide(id) {
	$("#" + id).hide();
}

function showAlert(title, type, msg) {
	// alert('a')
	$("body").append(
			'<div id="displays" style="display:none;z-index: 99999;" ></div>');
	var disinfo = '<div id = "displays_con"><div id="displays_rs"><div id="displays_img"></div></div><div id="displays_msg"><span id="displays_msg_title"></span><br><span id="displays_msg_info"></span></div></div>';
	$("#displays").html(disinfo);
	$("#displays").slideUp(300).fadeIn("slow");
	$("#displays_img").addClass("displays_img_" + type);
	$("#displays_msg_title").addClass("displays_msg_title_" + type);
	if (title != undefined) {
		$("#displays_msg_title").html(title);
	}
	if (msg != undefined) {
		$("#displays_msg_info").html(msg);
	}

	$("#displays").delay(1500).fadeOut("slow");

}
function showUrlInId(url, id, width, height) {
	getHtml(url, id);
	showWin(id, width, height);
}
function closeWin() {
	$.modal.close(); // must call this!
}
function showWin(id, width, height) {

	if (width == undefined || width == '') {
		var width = 400;
	}
	if (height == undefined || height == '') {
		var height = 200;
	}

	$("#" + id).modal({
		opacity : 30,
		maxHeight : height,
		maxWidth : width,
		overlayClose : true,
		containerCss : {
			backgroundColor : "#ffffff",
			borderColor : "#bbbbbb"
		},
		// overlayCss: {backgroundColor:"#fff"}

		onClose : function(dialog) {
			dialog.data.fadeOut('fast', function() {
				dialog.container.slideUp('fast', function() {
					dialog.overlay.fadeOut('fast', function() {
						$.modal.close(); // must call this!

					});
				});
			});
		}

	});

}
function showFrame(url, width, height) {
	var src = url;
	$.modal('<iframe src="' + src + '" height="' + height + '" width="' + width
			+ '" style="border:0">', {
		closeHTML : "",
		containerCss : {
			backgroundColor : "#fff",
			borderColor : "#fff",
			height : height,
			padding : 0,
			width : width
		},
		overlayClose : true
	});

}
function del(e) {

	var type = $(e).attr('del_type');
	if (type == 'one') {
		var id = $(e).attr('delete_id');

	} else {
		var id = getCheckItems();
	}
	var back = $(e).attr('back');
	var back_id = $(e).attr('back_id');
	if (id != '' && id != false) {
		if (confirm('您确认要删除吗?')) {
			$(e).attr("disabled", "disabled");
			$("#error").html('请稍候...');
			var url = $(e).attr("url");
			$.post(url, {
				'id' : id
			}, function(rs) {
				if (rs == '1') {
					showAlert('删除成功', '1');
					getHtml(back, back_id);

				} else {
					showAlert('删除失败', '0');
				}
				$(e).attr("disabled", "");
				return false;
			})
		} else {
			return false;
		}
	} else {
		return false;
	}
}
// 获取html文件URL参数
function getParameter(name, cancelBubble) {
	var r = new RegExp("(\\?|#|&)" + name + "=([^&#]*)(&|#|$)");
	var m = location.href.match(r);
	// if ((!m || m=="") && !cancelBubble) m = top.location.href.match(r);
	return (!m ? "" : m[2]);
}
// 获取字符长度
function getLen(str) {
	var totallength = 0;
	for ( var i = 0; i < str.length; i++) {
		// charCodeAt 返回一个整数，获得字符的unicode编码
		var intCode = str.charCodeAt(i);
		if (intCode >= 0 && intCode <= 128) {// 为字符的编码范围
			totallength = totallength + 1;// 非中文单个字符长度加1
		} else {
			totallength = totallength + 2;// 中文字符长度则加2
		}
	}
	return totallength;
}
function tourl(url) {
	window.location = url;

}
if (location.search != '') {
	var edit_url = location.href + '&do=edit&ajax=1';
} else {
	var edit_url = location.href + '?do=edit&ajax=1';
}
edit_url = edit_url.replace('index', 'modify');
var edit_td_status = 0;
function edit_td(e, col, id) {
	if (edit_td_status == 0) {
		edit_td_status = 1;
		var ihtml = $(e).html();
		if ($.trim(ihtml) == "" || $.trim(ihtml) == "&nbsp;&nbsp;") {
			var span_width = 100;
		} else {
			var span_width = e.scrollWidth + 2;
		}
		$(e).hide();
		$(e)
				.html(
						'<input type="text" style="width:'
								+ span_width
								+ 'px;" value="'
								+ ihtml
								+ '" id="'
								+ col
								+ id
								+ '"  onblur = "edit_td_save(this,\''
								+ col
								+ '\',\''
								+ id
								+ '\',\''
								+ ihtml
								+ '\')"  onkeydown="if(event.keyCode==13) edit_td_save(this,\''
								+ col + '\',\'' + id + '\',\'' + ihtml
								+ '\');"/>');
		$(e).show('fast');
		$("#" + col + id).focus();
	}

}
function edit_td_save(e, col, id, phtml) {
	var inputValue = $.trim($(e).val());
	if (inputValue == "") {
		edit_td_status = 0;
		$(e.parentNode).html(phtml)
		return false;
	}

	if (inputValue == $.trim(phtml)) {
		edit_td_status = 0;
		$(e.parentNode).html(phtml)
		return false;
	}
	var ihtml = $(e.parentNode).html();
	$(e.parentNode).html(inputValue)
	$.getJSON(edit_url, {
		'id' : id,
		'col' : col,
		'values' : inputValue
	}, function(rs) {
		// showAlert(rs.html)
		showMsg(rs.html);

	})
	edit_td_status = 0;
}

function orderBy(col, sc) {
	if (location.search != '') {
		var order_url = location.href;
		var reg = /orderby=[\w+]*[-.]*[\w+]*/;
		if (!reg.test(order_url)) {
			var new_order_url = order_url + "&orderby=" + col;
		} else {
			var new_order_url = order_url.replace(reg, "orderby=" + col);
		}
		order_url = new_order_url;

		var reg2 = /sc=[\w+]*/;
		if (!reg2.test(order_url)) {
			var new_order_url_two = order_url + "&sc=" + sc;
			order_url = new_order_url_two;
		} else {
			var new_order_url_two = order_url.replace(reg2, "sc=" + sc);
			order_url = new_order_url_two;
		}

	} else {
		var order_url = location.href + '?orderby=' + col + '&sc=' + sc;
	}
	window.location.href = order_url;
}
function GetQueryString(name) {

	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");

	var r = window.location.search.substr(1).match(reg);

	if (r != null)
		return unescape(r[2]);
	return null;

}
$(document).ready(function() {
	var orderby = GetQueryString('orderby');

	var sc = GetQueryString('sc');
	if (orderby) {
		var e = document.getElementById("order_" + orderby);
		var ehtml = e.innerHTML;
		if (sc == 'ASC') {

			e.innerHTML = ehtml + '↑';
			// append('↑')
			e.onclick = function() {
				orderBy(orderby, 'DESC')
			};

		} else {
			// .e.append('↓');
			e.innerHTML = ehtml + '↓';
			e.onclick = function() {
				orderBy(orderby, 'ASC')
			};
		}
	}
})

function showMsg(msg) {
	parent.$("#error").html(msg);
	setTimeout(function(e) {
		parent.$("#error").html('');
	}, 2000);
}

function reload() {

	setTimeout(function(e) {
		window.location.reload();
	}, 3000)
}

// jquery drag drop alert <a href="javascript:;" id="dragalert" title="123" content="444">打开弹出层</a>
var floatWrapX, floatWrapY, dragX, dragY, pX, pY, tX, tY;
var cX = document.documentElement.clientWidth;
var cY = document.documentElement.clientHeight;
var floatWrapPerX = floatWrapPerY = floatshowOne = 0;
var drag = false;
var divscroll = true; // 是否响应滚动条事件
var resizeswitch = true; // 是否响应窗体改变事件
$(document).ready(		
		function() {
			$("body").append('<div id="floatWrap" ><div id="floatWrapHead"></div><div id="floatWrapClose">&nbsp;</div> <div id="floatWrapMain">  </div></div>');
			$('#dragalert')
					.click(
							function() {
								$("#floatWrapHead").html($("#dragalert").attr("title"));
								$("#floatWrapMain").html($("#dragalert").attr("content"));
								if (!drag) {
									if (floatshowOne == 0) {
										floatWrapX = $(window).scrollLeft()
												+ (cX / 2)
												- ($('#floatWrap').outerWidth(
														true) / 2);
										floatWrapY = $(window).scrollTop()
												+ (cY / 2)
												- ($('#floatWrap').outerHeight(
														true) / 2);
										$('#floatWrap').css({
											top : floatWrapY - 100,
											left : floatWrapX
										}).show();
										pX = parseInt($('#floatWrap').css(
												"left"))
												- $(window).scrollLeft();
										pY = parseInt($('#floatWrap')
												.css("top"))
												- $(window).scrollTop();
										floatshowOne = 1;
									} else {
										$('#floatWrap').show();
										pX = parseInt($('#floatWrap').css(
												"left"))
												- $(window).scrollLeft();
										pY = parseInt($('#floatWrap')
												.css("top"))
												- $(window).scrollTop();
									}
									floatWrapPerX = pX / cX;
									floatWrapPerY = pY / cY;
								}
							});
			$(window).scroll(function() {
				if (!drag && divscroll) {
					floatWrapX = $(window).scrollLeft() + pX;
					floatWrapY = $(window).scrollTop() + pY;
					$('#floatWrap').css({
						top : floatWrapY,
						left : floatWrapX
					});
				}
			});
			$(window).resize(
					function() {
						if (!drag && resizeswitch) {
							cX = document.documentElement.clientWidth;
							cY = document.documentElement.clientHeight;
							floatWrapX = $(window).scrollLeft() + cX
									* floatWrapPerX;
							floatWrapY = $(window).scrollTop() + cY
									* floatWrapPerY;
							$('#floatWrap').css({
								top : floatWrapY,
								left : floatWrapX
							});
							pX = parseInt($('#floatWrap').css("left"))
									- $(window).scrollLeft();
							pY = parseInt($('#floatWrap').css("top"))
									- $(window).scrollTop();
						}
					});
			$('#floatWrapHead').mousedown(
					function(event) {
						$(this).css({
							cursor : 'move'
						});
						$('#floatWarpClone').remove();
						$('#floatWrap').clone(true).insertAfter('#floatWrap')
								.attr('id', 'floatWarpClone').show();
						$('body').bind("selectstart", function() {
							return false
						});
						$('#floatWrap').hide();
						dragX = ($(window).scrollLeft() + event.clientX)
								- (parseInt($('#floatWrap').css("left")));
						dragY = ($(window).scrollTop() + event.clientY)
								- (parseInt($('#floatWrap').css("top")));
						drag = true;
					});
			$('body').mousemove(function(event) {
				if (drag) {
					tX = event.pageX - dragX;
					tY = event.pageY - dragY;
					$('#floatWarpClone').css({
						left : tX,
						top : tY
					});
					pX = tX - $(window).scrollLeft();
					pY = tY - $(window).scrollTop();
					floatWrapPerX = pX / cX;
					floatWrapPerY = pY / cY;
				}
			});
			$('#floatWrapHead').mouseup(function() {
				$('#floatWrap').css({
					left : tX,
					top : tY
				});
				$('#floatWarpClone').remove();
				$('body').unbind("selectstart");
				$('#floatWrapHead').css({
					cursor : 'default'
				});
				$('#floatWrap').show();
				drag = false;
			});
			$('#floatWrapClose').click(function() {
				$('#floatWrap').hide();
				floatshowOne = 0;
			})
		})